(function () {
    function CountDown() {
        this.isStop = false;
        this.sec = 0;
        this.fn = undefined;
        this.every = 1;
        this.startTime = 0;
    }

    /**
     * 倒计时
     * @param sec 一共倒计时多少秒，从调用此函数开始倒计时
     * @param fn 倒计时结束和每隔多少秒回调的函数。调用此函数时，不会马上回调。回调参数是还剩余多少秒
     * @param every 设定每隔多少秒回调一次。默认1秒
     * */
    CountDown.prototype.start = function (sec,fn,every) {
        var t = this;
        t.sec = sec;
        t.fn = fn;
        t.every = every||1;
        t.isStop = false;
        t.startTime = new Date().getTime();

        t.interval();
    };
    CountDown.prototype.interval = function () {
        if(this.isStop)
            return;
        var t = this,
            nowTime = new Date().getTime(),
            remainSec = t.sec-Math.round((nowTime-t.startTime)/1000);

        if(remainSec>0){
            setTimeout(t.interval.bind(t),(remainSec<t.every?remainSec:t.every)*1000);
        }
        t.remainSec = remainSec;
        t.fn(remainSec)
    };
    CountDown.prototype.stop = function () {
        this.isStop = true;
    };


    /** Detect free variable `exports`. */
    var freeExports = typeof exports === 'object' && exports && !exports.nodeType && exports;
    /** Detect free variable `module`. */
    var freeModule = freeExports && typeof module === 'object' && module && !module.nodeType && module;

    // Check for `exports` after `define` in case a build optimizer adds it.
    if (freeModule) {
        // Export for Node.js.
        (freeModule.exports = CountDown).CountDown = CountDown;
        // Export for CommonJS support.
        freeExports.CountDown = CountDown;
    }else if(window){
        var H = window.H || {};
        H.CountDown = CountDown;
    }
}());
